﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;

namespace TipokHostManager.Configuration
{
    public class MachineElement : ConfigurationElement
    {
        private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty _ip = new ConfigurationProperty("ip", typeof(string), "0.0.0.0", ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _userName = new ConfigurationProperty("userName", typeof(string), string.Empty, ConfigurationPropertyOptions.None);
        private static readonly ConfigurationProperty _password = new ConfigurationProperty("password", typeof(string), string.Empty, ConfigurationPropertyOptions.None);
        private static readonly ConfigurationProperty _likeSearch = new ConfigurationProperty("likeSearch", typeof(bool), false, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _services = new ConfigurationProperty("services", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        static MachineElement()
        {
            _properties.Add(_ip);
            _properties.Add(_userName);
            _properties.Add(_password);
            _properties.Add(_services);
            _properties.Add(_likeSearch);
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        public string IP
        {
            get { return (string)base[_ip]; }
        }

        public string UserName
        {
            get { return (string)base[_userName]; }
        }

        public string Password
        {
            get { return (string)base[_password]; }
        }

        /// <summary>
        /// 是否使用like查询
        /// </summary>
        public bool LikeSearch
        {
            get { return (bool)base[_likeSearch]; }
        }

        /// <summary>
        /// 服务名集合
        /// </summary>
        public List<string> Services
        {
            get
            {
                var str = base[_services] as string;

                if (String.IsNullOrEmpty(str))
                {
                    return new List<string>();
                }
                return str.Split(';').ToList();
            }
        }
    }
}
